import { Router } from 'vue-router'
import { useLoginInfoStore } from '@/store'

/**
 * 路由守卫函数
 * @param router - 路由实例
 */
export function createRouterGuard(router: Router) {
  router.beforeEach((to: any, from: any, next: any) => {
    const loginInfoStore = useLoginInfoStore()
    const token = loginInfoStore.loginInfo.token
    if (token) {
      /** 这段代码就是为了做两个端的页面权限控制 如果有问题就注释掉它 start */
      if (to.path === '/' || to.path === '/userLogin') {
        return next()
      }
      if (loginInfoStore.loginInfo.loginType === 1 && to.path !== '/jgPersonnelInfo') {
        return next({ path: '/' })
      } else if (loginInfoStore.loginInfo.loginType === 2 && to.path === '/jgPersonnelInfo') {
        return next({ path: '/userLogin' })
      }
      /** 这段代码就是为了做两个端的页面权限控制 如果有问题就注释掉它 end */
      next()
    } else {
      if (to.path === '/' || to.path === '/userLogin') {
        return next()
      }
      from.path === '/userLogin' ? next({ path: '/userLogin' }) : next({ path: '/' })
    }
  })
  router.afterEach((to: any) => {})
}
